package com.yanqu.road.dao.impl.xiaomian;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.xiaomian.PlatformDailyData;
import com.yanqu.road.entity.xiaomian.UserXiaoMianData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class XiaoMianDaoImpl extends TempDataObjectDao<UserXiaoMianData> {

    public UserXiaoMianData getUserXiaoMianData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_xiaomian where userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    @Override
    protected boolean addData(UserXiaoMianData data) {
        PreparedStatement call = null;
        String sql = "insert into t_u_xiaomian(userId,resetTime,patronsUpgradeTimes,beautyTrystTimes,childTrainTimes,innCookTimes,marriageTimes,"
                + "banquetScore,clinicReception,wineShopCallNumber,ingotsConsume,dailyData) values (?,?,?,?,?,?,?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setLong(i++, data.getResetTime());
            call.setLong(i++, data.getPatronsUpgradeTimes());
            call.setLong(i++, data.getBeautyTrystTimes());
            call.setLong(i++, data.getChildTrainTimes());
            call.setLong(i++, data.getInnCookTimes());
            call.setLong(i++, data.getMarriageTimes());
            call.setLong(i++, data.getBanquetScore());
            call.setLong(i++, data.getClinicReception());
            call.setLong(i++, data.getWineShopCallNumber());
            call.setLong(i++, data.getIngotsConsume());
            call.setString(i++, data.getDailyDataJsonStr());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(UserXiaoMianData data) {
        PreparedStatement call = null;
        String sql = "update t_u_xiaomian set resetTime=?,patronsUpgradeTimes=?,beautyTrystTimes=?,childTrainTimes=?,innCookTimes=?,marriageTimes=?,"
                + "banquetScore=?,clinicReception=?,wineShopCallNumber=?,ingotsConsume=?,dailyData=? where userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getResetTime());
            call.setLong(i++, data.getPatronsUpgradeTimes());
            call.setLong(i++, data.getBeautyTrystTimes());
            call.setLong(i++, data.getChildTrainTimes());
            call.setLong(i++, data.getInnCookTimes());
            call.setLong(i++, data.getMarriageTimes());
            call.setLong(i++, data.getBanquetScore());
            call.setLong(i++, data.getClinicReception());
            call.setLong(i++, data.getWineShopCallNumber());
            call.setLong(i++, data.getIngotsConsume());
            call.setString(i++, data.getDailyDataJsonStr());
            call.setLong(i++, data.getUserId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected UserXiaoMianData parseData(ResultSet rs) throws SQLException {
        UserXiaoMianData data = new UserXiaoMianData();
        data.setUserId(rs.getLong("userId"));
        data.setResetTime(rs.getLong("resetTime"));
        data.setPatronsUpgradeTimes(rs.getLong("patronsUpgradeTimes"));
        data.setBeautyTrystTimes(rs.getLong("beautyTrystTimes"));
        data.setChildTrainTimes(rs.getLong("childTrainTimes"));
        data.setInnCookTimes(rs.getLong("innCookTimes"));
        data.setMarriageTimes(rs.getLong("marriageTimes"));
        data.setBanquetScore(rs.getLong("banquetScore"));
        data.setClinicReception(rs.getLong("clinicReception"));
        data.setWineShopCallNumber(rs.getLong("wineShopCallNumber"));
        data.setIngotsConsume(rs.getLong("ingotsConsume"));
        String dailyDataStr = rs.getString("dailyData");
        if(StringUtils.isNullOrEmpty(dailyDataStr)){
            dailyDataStr = "{}";
        }
        PlatformDailyData platformDailyData = JSON.parseObject(dailyDataStr, new TypeReference<PlatformDailyData>() {});
        data.setDailyData(platformDailyData);
        return data;
    }
}
